package org.xmap.ui.control;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import org.xmap.app.Env;
import org.xmap.ui.Theme;
import org.xmap.ui.UIUtil;
import org.xmap.ui.ViewManager;
import org.xmap.ui.base.Event;

public final class IList extends ListBase {

    public IList(int leftUpX, int leftUpY, int width, int height, int orient) {
        super(leftUpX, leftUpY, width, height, orient);
        this.disCount = (height / Env.instance.defaultFontHeight) - 1;
        if (height % this.spacing != 0) {
            this.disCount++;
        }
        this.disOffset = 0;
        this.spacing = Env.instance.defaultFontHeight;
//        this.showTitle = true;
//        this.setclipBeforeDraw = true;
        super.setSetClipBeforeDraw(true);
        super.setShowTitle(true);
    }

    public void addContent(int id, Object obj,
                           Object first1, Object first2,
                           Object second1, Object second2,
                           Object third1, Object third2, boolean isHighlight) {
        ListItem i = new ListItem(id, obj, first1, first2, second1, second2, third1, third2, isHighlight);
        this.content.addElement(i);
        if (this.index == -1) {
            this.index = 0;
            this.pressSelectedIndex = index;
            this.disOffset = 0;
        }
        if (this.content.size() == 1 && this.itemListener != null) {
            this.notifyItemListener();
        }
        this.cellHeightInStrollbar = (this.getRect().height - 2) / this.content.size();
    }

    public void insertContent(int index, int id, Object obj,
                              Object first1, Object first2,
                              Object second1, Object second2,
                              Object third1, Object third2, boolean isHighlight) {
        ListItem i = new ListItem(id, obj, first1, first2, second1, second2, third1, third2, isHighlight);
        this.content.insertElementAt(i, index);
        if (this.index == -1) {
            this.index = 0;
            this.disOffset = 0;
            this.pressSelectedIndex = index;
        }
        this.notifyItemListener();
        this.cellHeightInStrollbar = (this.getRect().height - 2) / this.content.size();
    }

    public void removeContent(Object obj) {
        Object o = this.getSelectObj();
        if (o == obj) {
            this.removeSelected();
        } else {
            for (int i = 0; i < this.content.size(); i++) {
                ListItem li = (ListItem)this.content.elementAt(i);
                if (li.obj == obj) {
                    this.content.removeElement(li);
                    if (i < this.drawBase) {
                        this.drawBase--;
                    }
                    if (i >= this.drawBase && i < (this.drawBase + this.index)) {
                        this.index--;
                    }
                    if (i > (this.drawBase + this.index) && i < (this.drawBase + this.disCount - 1)) {
                        // do nothing
                    }
                    if (i > this.drawBase + this.disCount) {
                        // do nothing
                    }
                    if (this.content.size() > 0) {
                        this.cellHeightInStrollbar = (this.getRect().height - 2) / this.content.size();
                    }
                    break;
                }
            }
        }
    }

    public boolean pointerEvent(Event ev) {
        boolean result = false;
        switch (ev.type) {
        case Event.POINTER_PRESSED:
            if (this.getRect().collideWith(ev.param1, ev.param2)) {
                super.setPointerSelected(true);
                this.pointerPressedY = ev.param2; // 记录触笔的垂直位置
                int y = ev.param2 - this.getRect().leftUp.y - (this.showTitle ? this.getTitleHeight() : 0) /* - this.disOffset*/;
                if (this.adjust) {
                    y += this.disOffset;
                }
                int curIndex = y / this.spacing;
                if (curIndex >= 0 && curIndex < this.content.size()) {
                    ListItem li = (ListItem)this.content.elementAt(curIndex);
                    if (li != null) {
                        int w = Env.instance.defaultFont.stringWidth((String) li.first1);
                        if (ev.param1 < w) { // 只有当前的y坐标在文字的宽度以内
                            this.index = curIndex;
                            if (!super.getFocus()) {
                                if (this.owner != null) {
                                    this.owner.requestGetFocus(this);
                                }
                            }
                            if (this.itemListener != null) {
                                this.itemListener.pointerSelected(li.id, li.obj);
                            }
                        }
                    }
                } else {
                    if (!super.getFocus()) {
                        if (this.owner != null) {
                            this.owner.requestGetFocus(this);
                        }
                    }
                }
                ViewManager.instance.requestRepaint();
                result = true;
            }
            break;
        case Event.POINTER_RELEASED:
            if (super.getPointerSelected()) {
                super.setPointerSelected(false);
                if (this.getRect().collideWith(ev.param1, ev.param2)) {
                    int y = ev.param2 - this.getRect().leftUp.y - (this.showTitle ? this.getTitleHeight() : 0);
                    if (this.adjust) {
                        y += this.disOffset;
                    }
                    int curIndex = y / this.spacing;
                    if (curIndex >= 0 && curIndex < this.content.size() && this.index == curIndex) {
                        ListItem li = (ListItem)this.content.elementAt(curIndex);
                        if (li != null) {
                            int w = Env.instance.defaultFont.stringWidth((String) li.first1);
                            if (ev.param1 < w) { // 只有当前的y坐标在文字的宽度以内
                                if (this.itemListener != null) {
                                    if (super.pressSelectedIndex == curIndex) {
                                        this.itemListener.pointerDoubleSelected(li.id, li.obj);
                                    }
                                    int distance = ev.param2 - this.pointerPressedY;
                                    if (distance < 3 || distance > -3) {
                                        this.itemListener.fireSelected(li.id, li.obj);
                                    }
                                }
                            }
                        }
                    }
                    super.pressSelectedIndex = this.getDisplayIndex();
                }
                result = true;
            }
            break;
        case Event.POINTER_DRAGGED:
            if (super.getPointerSelected()) {
                if (this.getRect().collideWith(ev.param1, ev.param2)) {
                    int y = ev.param2 - this.pointerPressedY;
                    this.disOffset += y;
//                    this.disOffset -= y;
                    this.pointerPressedY = ev.param2;
                    System.out.println(this.disOffset);
                    if (this.disOffset < 0) {
                        this.disOffset = 0;
                    } else {
                        int i = this.content.size() * this.spacing - this.getRect().height;
                        if (i > 0) {
                            if (this.disOffset > i) {
                                this.disOffset = i;
                            }
                        } else {
                            this.disOffset = 0;
                        }
                    }
                    ViewManager.instance.requestRepaint();
                    result = true;
                }
            }
            break;
        }
        return result;
    }

    public void draw(Graphics g) {

        g.setFont(Env.instance.defaultFont);
        g.setColor(this.bgColor);
        g.fillRect(0, 0, this.getRect().width, this.getRect().height);

        if (Theme.instance.listBackgroundImage != null) {
            g.drawImage(Theme.instance.listBackgroundImage, 0, 0, Graphics.LEFT | Graphics.TOP);
        }

        int base = this.showTitle ? this.titleHeight : 0;
        g.setColor(Theme.instance.listLineColor);
        if (this.adjust) {
            base -= this.disOffset;
        }

//        g.drawLine(0, base - 1, this.getRect().width, base - 1);

        for (int i = 0; i < this.content.size(); i++) {
            if (i == this.index) {
                g.setColor(Theme.instance.listSelectedBackgroundColor);
                g.fillRect(0, base, this.getRect().width, this.spacing);
            }
            ListItem item = (ListItem)this.content.elementAt(i);
            if (item != null) {
                int rowMid = base + (this.spacing >> 1);
                base += this.spacing;

                if (rowMid > -this.spacing && rowMid < this.viewHeight + this.spacing) {
                    if (!item.isHighlight) {
                        g.setColor(Theme.instance.listTextColor);
                    } else {
                        g.setColor(Theme.instance.listHighLightTextColor);
                    }
                    if (this.showNumber) {
                        int h = i + 1;
                        if (h <= 9) {
                            UIUtil.draw(g, "0" + h, 1, rowMid, Graphics.LEFT, Graphics.VCENTER);
                        } else {
                            UIUtil.draw(g, String.valueOf(h), 1, rowMid, Graphics.LEFT, Graphics.VCENTER);
                        }
                    }
                    this.drawItem(item.first1, g, this.columnX1, rowMid, this.columnAnchor1);
                }
//                g.setColor(Theme.instance.listLineColor);
//                g.drawLine(0, base - 1, this.getRect().width, base - 1);
            }
        }

//        g.drawLine(0, base - 1, this.getRect().width, base - 1);

        int y1 = 0;
        int y2 = 0;
        if (this.content.size() * this.spacing < this.getRect().height) {
            y1 = 1;
            y2 = this.getRect().height - 2;
        } else {
            y1 = (this.disOffset * (this.getRect().height - 2)) / (this.content.size() * this.spacing);
            y2 = (this.disOffset + this.getRect().height) * (this.getRect().height - 2) / (this.content.size() * this.spacing);
        }

        g.setColor(Theme.instance.explorerStrollbarBackgroundColor);
        g.fillRect(this.getRect().width - 4, 0, 4, this.getRect().height);
        g.setColor(Theme.instance.explorerStrollbarColor);
        g.fillRect(this.getRect().width - 3, y1, 2, y2 - y1 + 1);
    }

    public void upSelectIndex() {
        if (this.index > 0) {
            this.index--;
            if (this.index == 0) {
                this.disOffset = 0;
            }
        } else {
            this.index = this.content.size() - 1;
            this.disOffset = (this.index + 1) * this.spacing - this.viewHeight;
            int i = this.content.size() * this.spacing - this.getRect().height;
            if (i > 0) {
                if (this.disOffset > i) {
                    this.disOffset = i;
                }
            } else {
                this.disOffset = 0;
            }
        }
        if (this.index * this.spacing < this.disOffset) {
            this.disOffset = this.index * this.spacing;
        }
        this.pressSelectedIndex = index;
        this.notifyItemListener();
        ViewManager.instance.requestRepaint();
    }

    public void downSelectIndex() {
        if (this.index < this.content.size() - 1) {
            this.index++;
            if (this.index == this.disCount - 1) {
                this.disOffset = this.spacing - (this.viewHeight % this.spacing);
            }
        } else {
            this.index = 0;
            this.disOffset = 0;
        }
        if ((this.index + 1) * this.spacing - this.disOffset > this.viewHeight) {
            this.disOffset += this.spacing;
        }
        this.pressSelectedIndex = index;
        this.notifyItemListener();
        ViewManager.instance.requestRepaint();
    }

    private void drawItem(Object item, Graphics g, int x, int y, int anchor) {
        if (item != null) {
            if (item instanceof String) {
                UIUtil.draw(g, (String) item, x, y, this.getMidpAnchor(anchor), Graphics.VCENTER);
            } else if (item instanceof Image) {
                g.drawImage((Image) item, x, y, this.getMidpAnchor(anchor) | Graphics.VCENTER);
            } else if (item instanceof DrawableItem) {
                Object obj = ((DrawableItem) item).getDrawableItem();
                if (obj != null) {
                    if (obj instanceof String) {
                        UIUtil.draw(g, (String) obj, x, y, this.getMidpAnchor(anchor), Graphics.VCENTER);
                    } else if (obj instanceof Image) {
                        g.drawImage((Image) obj, x, y, this.getMidpAnchor(anchor) | Graphics.VCENTER);
                    }
                }
            }
        }
    }
}
